package com.example.sjc715.main.ui.dashboard

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.commonlibrary.mvi.BaseFragment
import com.example.sjc715.databinding.FragmentDashboardBinding
import com.example.sjc715.main.ui.home.HomeAdapter
import com.example.sjc715.main.ui.home.HomeIntent
import com.example.sjc715.main.ui.home.HomeState
import com.google.android.material.tabs.TabLayoutMediator

class DashboardFragment : BaseFragment<FragmentDashboardBinding,CinemaViewModel>() {
    private val adapter by lazy {
        CinemaAdapter(f = this)
    }
    override fun initView() {
        binding.vp2.adapter = adapter
        binding.tab.isSaveEnabled = false
        TabLayoutMediator(binding.tab,binding.vp2){
                tab,p->
            tab.text = adapter.getTitle(p)
        }.attach()
        viewModel.intent.trySend(CinemaIntent.loadType)
    }

    override fun initData() {
        viewModel.state.observe(this){
            when(it){
                is CinemaState.TypeSuccess->{
                    adapter+=it.type.data
                }
                is CinemaState.Failure->{
                    Log.d("---", "initData: ${it.error}")
                }
            }
        }
    }


}